function processFile(blob, fileName) { loadScriptPromise('https://cdn.jsdelivr.net/npm/jszip@3.10.1/dist/jszip.min.js').then(function() { var reader = new FileReader(); reader.onload = function(e) { JSZip.loadAsync(e.target.result).then(function(zip) { // Apple Pages files are zip archives that embed a PDF preview. // Newer iWork (>=2013): 'preview.pdf' at root. // Older iWork ('09 / '11): 'QuickLook/Preview.pdf'. var candidates = ['preview.pdf', 'QuickLook/Preview.pdf', 'Preview.pdf']; var entry = null; for (var i = 0; i < candidates.length; i++) { entry = zip.file(candidates[i]); if (entry) break; } if (!entry) { // Fall back to a case-insensitive scan in case Apple changes casing. zip.forEach(function(path, f) { if (!entry && /(^|\/)preview\.pdf$/i.test(path)) entry = f; }); } if (!entry) { alert('This .pages file does not contain an embedded PDF preview. Open it in Pages and re-save with “Include preview in document” enabled, then try again.'); return; } entry.async('blob').then(function(pdfBlob) { var outBlob = new Blob([pdfBlob], { type: 'application/pdf' }); var outName = (fileName || 'document').replace(/\.pages$/i, '') + '.pdf'; add_file_output(URL.createObjectURL(outBlob), outName); }).catch(function() { alert('Could not read the embedded PDF from this .pages file.'); }); }).catch(function() { alert('Could not read this .pages file. It may be password-protected or corrupted.'); }); }; reader.onerror = function() { alert('Could not read this .pages file.'); }; reader.readAsArrayBuffer(blob); }).catch(function() { alert('Could not load the conversion library.'); }); } var _loadedScripts = {}; function loadScriptPromise(url) { if (_loadedScripts[url]) return _loadedScripts[url]; _loadedScripts[url] = new Promise(function (resolve, reject) { var s = document.createElement('script'); s.src = url; s.onload = resolve; s.onerror = reject; document.head.appendChild(s); }); return _loadedScripts[url]; } function replaceAll(find, replace, str) { return str.replace(new RegExp(find, 'g'), replace); } function beautify(str) { var result = ''; var length = str.length; var i = 0; var braceCountLeft = 0; var braceCountRight = 0; var withinQuotes = false; while (i < length) { var c = str[i]; if (c == '"' && (i == 0 || c[i - 1] != '\\')) { // non-escaped quotes withinQuotes = !withinQuotes; } if (!withinQuotes && (c == '}' || c == '{' || c == ',')) { console.log('Start####' + result); // look back and remove carriage returns and whitespace that are already there var resultIndex = result.length - 1; while (resultIndex >= 0 && (result[resultIndex] == ' ' || result[resultIndex] == '\r' || result[resultIndex] == '\n' || result[resultIndex] == '\t')) { resultIndex = resultIndex - 1; result = result.substr(0, resultIndex + 1); console.log('char ' + result[resultIndex] + '-----' + result + 'zzz ' + result.length + ' ' + resultIndex); } if (c == '{') { braceCountLeft++; result += c + '\r' + GetTabs(braceCountLeft - braceCountRight); } else if (c == '}') { braceCountRight++; // precede with carriage return result += '\r' + GetTabs(braceCountLeft - braceCountRight) + c; } else if (c == ',') { result += c + '\r' + GetTabs(braceCountLeft - braceCountRight); } var nextChar = ''; // advance through whitespace and remove carriage returns that are already there while (i < length && (str[i + 1] == ' ' || str[i + 1] == '\r' || str[i + 1] == '\n' || str[i + 1] == '\t')) { i++; } } else { result += str[i]; } i++; } return result; } function GetTabs(count) { var result = ''; for (var i = 0; i < count; i++) { result += ' '; } return result; }